package net.lab1024.sa.admin.module.app.service;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.app.req.AppOrderListReq;
import net.lab1024.sa.admin.module.app.rsp.AppUserBindListRsp;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.DollEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.OrderEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.UserDollBindEntity;
import net.lab1024.sa.base.common.domain.RequestUser;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author guolong.zhang
 * @date 2025/02/11 10:20
 **/
@Service
@Slf4j
public class AppUserService {


    public List<AppUserBindListRsp> userBindList(RequestUser requestUser) {
        List<AppUserBindListRsp> rspList = new ArrayList<>();
        List<UserDollBindEntity> list = Db.lambdaQuery(UserDollBindEntity.class)
                .eq(UserDollBindEntity::getUserId, requestUser.getUserId())
                .list();
        for (UserDollBindEntity userDollBind : list) {
            AppUserBindListRsp rsp = new AppUserBindListRsp();
            OrderEntity order = Db.lambdaQuery(OrderEntity.class)
                    .eq(OrderEntity::getOrderNo, userDollBind.getOrderNo())
                    .last("limit 1")
                    .one();
            DollEntity doll = Db.lambdaQuery(DollEntity.class)
                    .eq(DollEntity::getDollNo, order.getDollNo())
                    .one();
            if(doll != null){
                rsp.setImageUrl(doll.getImgUrl());
            }
            rsp.setId(userDollBind.getId());
            rsp.setOrderNo(order.getOrderNo());
            rsp.setDollNo(order.getDollNo());
            rsp.setCreateTime(userDollBind.getCreateTime());
            rspList.add(rsp);
        }
        return rspList;
    }
}
